<?php

namespace Modules\Buy\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Buy\Emails\YzmMail;
use Modules\Buy\Entities\Adress;
use Modules\Buy\Entities\Card;
use Modules\Buy\Entities\Collection;
use Modules\Buy\Entities\Foot;
use Modules\Buy\Entities\Good;
use Modules\Buy\Entities\Yonghu;
use Modules\Buy\Http\Requests\AdressRequest;
use Modules\Buy\Http\Requests\HuanbangRequest;
use Modules\Buy\Http\Requests\PeopleRequest;
use Modules\Buy\Http\Requests\SafeRequest;
use Modules\Buy\Http\Requests\ShimingRequest;
use Modules\Buy\Http\Requests\XiumimaRequet;

class PeopleController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
//    个人中心首页
    public function index()
    {
        $card = Card::where('user_id',\Auth::guard('shop')->user()->id)->get();

//        dd(123);

//        获得数据库最后添加的商品，个人中心页面我是新品要用
        $newpin = Good::orderBy('id', 'desc')->first();
//        dd($newpin->toArray());
//        获得销量最高的那个，个人中心要展示
        $remai = Good::where('iscommend',1)->first();
//        dd($remai->toArray());

//        从数据库里获得收藏的商品
        $coll = Collection::where('user_id',\Auth::guard('shop')->user()->id)->pluck('good_id');
//        dd($coll);
        $good = Good::whereIn('id',$coll)->limit(8)->get();
//        dd($good->toArray());
        return view('user.index',compact('newpin','remai','card','good'));

    }


//    个人信息页面
    public function userinfo()
    {
        $card = Card::where('user_id',\Auth::guard('shop')->user()->id)->get();

//        获取数据库里个人信息值，然后前台编辑显示
        $people = Yonghu::where('email',\Auth::guard('shop')->user()->email)->first();
//        dd($people->toArray());

//        dd(123);
        return view('user.userinfo',compact('people','card'));
    }


//    表单提交方法
    public function changeinfo(Request $request,PeopleRequest $peopleRequest)
    {

//        dd(123);
//        dd($request->all());
//        获得用户数据然后填充
        $post = $request->all();
//        dd($post['nickname']);
        $year = $request->input('year');
//        dd($year);
        $mouth = $request->input('mouth');
        $day = $request->input('day');
        $birthday = $year.'-'.$mouth.'-'.$day;
//        dd($birthday);
        Yonghu::where('email',\Auth::guard('shop')->user()->email)->update([
            'nickname'=>$post['nickname'],
            'sex'=>$post['sex'],
            'birthday'=>$birthday,
            'tel'=>$post['tel']
        ]);

        return view('dlzc.jump')->with([
            'message'=>'修改成功',
            'url'=>'people',
            'jumpTime'=>4,
            'name'=>'个人中心'
        ]);
    }


//    加载安全设置的页面路由
    public function security(){
        $card = Card::where('user_id',\Auth::guard('shop')->user()->id)->get();

//        获取用户数据邮箱
        $email = Yonghu::where('email',\Auth::guard('shop')->user()->email)->first();
//        dd($email);
        return view('user.security',compact('email','card'));
    }

//    配置修改密码路由
    public function mima()
    {
        $card = Card::where('user_id',\Auth::guard('shop')->user()->id)->get();
        return view('user.mima',compact('card'));
    }
//    修改密码表单提交路由
    public function xiumima(Request $request,XiumimaRequet $xiumimaRequet)
    {
//        dd(123);
//        dd($request->all());
        $post = $request->all();
//        dd($post);
//        dd(bcrypt($post['oldpassword']));
//        dd(bcrypt($post['oldpassword']));
        if (\Hash::check($post['oldpassword'],\Auth::guard('shop')->user()->password)) {
//            dd('密码正确');
//            如果密码匹配就变更数据库当中的密码
            Yonghu::where('email',\Auth::guard('shop')->user()->email)->update([
               'password'=>bcrypt($post['password'])
            ]);

            return view('dlzc.jump')->with([
                'message'=>'密码修改成功',
                'url'=>'security',
                'jumpTime'=>4,
                'name'=>'登录'
            ]);

        }else{
//            dd('密码不正确');

            return view('dlzc.jump')->with([
                'message'=>'原密码不匹配，请重新输入',
                'url'=>'mima',
                'jumpTime'=>4,
                'name'=>'修改密码'
            ]);
        }
    }

//    加载邮箱
    public function fayoujian()
    {
        $card = Card::where('user_id',\Auth::guard('shop')->user()->id)->get();

        $email = Yonghu::where('email',\Auth::guard('shop')->user()->email)->first();
//        dd(123);
        return view('user.email',compact('email','card'));
    }
    
//    配置获取验证码路由
    public function yzm(Request $request)
    {
//        return 123;
//        $post = $request->input('email');
        $user = Yonghu::where('email',\Auth::guard('shop')->user()->email)->first();
//        return $post;
//        接收到email，然后开始往邮箱发验证码
//        写一个四个字验证码，然后传过去
        $char_array = array_merge(range('A','Z'),range(0,9));
        $index = array_rand($char_array,5);
        shuffle($index);
        //拼接验证码字符串
        $str = '';
        foreach($index as $i){
            $str .= $char_array[$i];
        }
//        dd($str);
        \Mail::to($user)->send(new YzmMail($str));
        //将生成的随机字符串保存到会话中
//        session_start();
//        $_SESSION['code'] = $str;
//        return $str;
//        return 123;
//        return view('dlzc.jump')->with([
//            'message'=>'邮件已经发送，请登录QQ邮箱查看',
//            'url'=>'fayoujian',
//            'jumpTime'=>4,
//            'name'=>'换绑'
//        ]);

        return ['message'=>'邮件已经发送，请登录QQ查看'];

    }

    public function huanbang(HuanbangRequest $huanbangRequest)
    {
//        dd(123);
        return view('dlzc.jump')->with([
            'message'=>'更换邮箱成功',
            'url'=>'security',
            'jumpTime'=>4,
            'name'=>'安全设置'
        ]);
    }

//    实名认证
    public function idcard()
    {
        $card = Card::where('user_id',\Auth::guard('shop')->user()->id)->get();

        $user = Yonghu::where('email',\Auth::guard('shop')->user()->email)->first();

//        dd(123);
        return view('user.idcard',compact('user','card'));
    }

//    实名认证提交路由
    public function shiming(ShimingRequest $shimingRequest)
    {
//        dd(13);
        //        dd(123);
        return view('dlzc.jump')->with([
            'message'=>'实名认证成功',
            'url'=>'security',
            'jumpTime'=>4,
            'name'=>'安全设置'
        ]);
    }

//    安全问题视图
    public function safe()
    {
        $card = Card::where('user_id',\Auth::guard('shop')->user()->id)->get();

//        dd(123);
        return view('user.safe',compact('card'));
    }
    
//    安全验证表单提交
    public function safeyz(SafeRequest $safeRequest)
    {
//        dd(13);
        return view('dlzc.jump')->with([
            'message'=>'安全问题设置成功',
            'url'=>'security',
            'jumpTime'=>4,
            'name'=>'安全设置'
        ]);
    }

//    终于到地址的增删改查了
    public function adress()
    {
        $card = Card::where('user_id',\Auth::guard('shop')->user()->id)->get();

        $adress = Adress::where('yonghu_id',\Auth::guard('shop')->user()->id)->get();
//        dd(13);
//        dd($adress->toArray());
        return view('user.address',compact('adress','card'));
    }

    public function moren(Adress $adress,$id)
    {
//        return $id;
//        找到对应的地址，然后把它的状态变为1.
        Adress::where('yonghu_id', auth('shop')->user()->id)->update(['status' => 0]);
//        $res = $adress->select('status')->get();
//        dd($res);

        $res = Adress::find($id);
        $res->update([
            'status'=>1
        ]);
        return ['message'=>'变为默认成功'];
    }
    
    
    
    
    
//    地址修改成功的跳转
    public function newadress(Request $request,AdressRequest $adressRequest)
    {
//        dd($request->all());
        $post = $request->all();
//        dd($post);
        $adress = $post['sheng'].'-'.$post['shi'].'-'.$post['qu'];
//        dd($adress);
        Adress::create([
           'name'=>$post['name'],
           'tel'=>$post['tel'],
            'adress'=>$adress,
            'moreadress'=>$post['moreadress'],
            'yonghu_id'=>\Auth::guard('shop')->user()->id
        ]);

        return view('dlzc.jump')->with([
            'message'=>'地址添加成功',
            'url'=>'adress',
            'jumpTime'=>4,
            'name'=>'添加地址'
        ]);
    }

//    配置编辑视图
    public function bianji($id){

        $card = Card::where('user_id',\Auth::guard('shop')->user()->id)->get();

//        dd($id);
       $adress =  Adress::where('id',$id)->find($id);
//        dd($adress->toArray());
        return view('user.bianji',compact('adress','card'));
    }
    
//    配置编辑表单提交路由
    public function edit(AdressRequest $request,Adress $adress)
    {
//        dd($request->all());
        $post = $request->all();
//        dd($post);
        $ress = $post['sheng'].'-'.$post['shi'].'-'.$post['qu'];
//        dd($adress);
        Adress::where('id',$post['adress_id'])->update([
           'name'=>$post['name'],
            'tel'=>$post['tel'],
            'adress'=>$ress,
            'moreadress'=>$post['moreadress'],
            'yonghu_id'=>\Auth::guard('shop')->user()->id
        ]);
//        $adress->save();
        return view('dlzc.jump')->with([
            'message'=>'更新成功',
            'url'=>'adress',
            'jumpTime'=>4,
            'name'=>'添加地址'
        ]);
    }

//    删除地址：
    public function del($id)
    {
//        dd($id);
        $adress = Adress::find($id);
//        dd($adress->toArray());
        $adress->delete();

        return view('dlzc.jump')->with([
            'message'=>'删除成功',
            'url'=>'adress',
            'jumpTime'=>3,
            'name'=>'添加地址'
        ]);
    }

//    收藏视图
    public function cang()
    {

        $card = Card::where('user_id',\Auth::guard('shop')->user()->id)->get();

//        获得收藏表里的所有数据循环展示到首页
        $coll = Collection::where('user_id',\Auth::guard('shop')->user()->id)->pluck('good_id');
//        dd($coll);
        $good = Good::whereIn('id',$coll)->get();
        
        return view('user.collection',compact('card','good'));
    }


//    足迹
    public function foot()
    {
//        dd(123);
        $card = Card::where('user_id',\Auth::guard('shop')->user()->id)->get();

        $coll = Foot::where('user_id',\Auth::guard('shop')->user()->id)->pluck('good_id');
//        dd($coll);
        $good = Good::whereIn('id',$coll)->orderBy('id','desc')->limit(10)->get();

        return view('user.foot',compact('card','good'));
    }

//    添加到足迹路由
    public function zuji($goodid)
    {
//        dd(123);
        if (\Auth::guard('shop')->check()){
            Foot::create([
                'user_id'=>\Auth::guard('shop')->user()->id,
                'good_id'=>$goodid
            ]);
        }
        return ['volid'=>1,'message'=>'已经收藏到足迹'];
    }


//    足迹删除
    public function chu($id)
    {
//        dd($id);
//        找到对应的足迹，然后删除
        Foot::where('good_id',$id)->delete();
        return ['volid'=>1,'message'=>'删除成功'];

    }

//    取消收藏
    public function cangqu($id)
    {
//        dd($id);
        Collection::where('good_id',$id)->delete();
        return ['volid'=>1,'message'=>'取消成功'];
    }    


}
